package com.vint.lintcode;

import java.util.Stack;

/**
 * 两栈实现队列
 */
public class Lintcode40 {
	Stack<Integer> in ;
	Stack<Integer> out ;

	public Lintcode40() {
		// do intialization if necessary
		in = new Stack<>();
		out = new Stack<>();
	}

	/*
	 * @param element: An integer
	 * @return: nothing
	 */
	public void push(int element) {
		// write your code here
		if (!out.empty()){
			revers(in, out);
		}
		in.push(element);
	}

	/*
	 * @return: An integer
	 */
	public int pop() {
		// write your code here
		if (!in.empty()) {
			revers(out, in);
		}
		return out.pop();
	}

	/*
	 * @return: An integer
	 */
	public int top() {
		// write your code here
		if(!in.empty()) {
			revers(out, in);
		}
		return out.peek();
	}

	public void revers(Stack<Integer>in, Stack<Integer>out) {
		while(!out.empty()){
			in.push(out.pop());
		}
	}

}
